package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import conn.ConData;
import model.Buildlog;

/*
 * 对buildlog表进行各种操作
 * */
public class BuildlogDao {
	Connection con=null;//连接数据库对象  默认值为空
	ConData conn=new ConData();//连接数据库驱动

	/**
	 * 给buildlog表添加数据
	 * @param buildlog 实体
	 * @return
	 */
	public int add(Buildlog buildlog){//参数类型 参数名  向方法里传参
		int num=0;//添加不成功
		//创建sql语句
//		String sql="INSERT INTO buildlog (projectprincipal) VALUE(?)";
		String sql="INSERT INTO buildlog (buildingsite,begintime,endtime,projectprincipal,price,constructionorganization,constructionorganizationcontacts,accessory,isagree,replenishaccessory,replenishreason,replenishtime,agreetime,changemoney,lastendtime,agreedoc,state,statedetail,ischange,safepollingdoc,changereason,changeendtime,checktime,checker,checkmaterials) VALUE(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		//创建执行sql的对象A
				PreparedStatement ps;//带参数的执行sql语句对象
				try {
					//输出buildlog的项目负责人方法的值
					System.out.println(buildlog.getProjectprincipal());
					//调用连接数据库的方法
					con=conn.getCon();
					//返回一个PreparedStatement实例
					ps=con.prepareStatement(sql);
					

					//设置查询条件

					ps.setString(1, buildlog.getBuildingsite());
					ps.setString(2, buildlog.getBegintime());
					ps.setString(3, buildlog.getEndtime());
					ps.setString(4 , buildlog.getProjectprincipal());
					ps.setString(5, buildlog.getPrice());
					ps.setString(6, buildlog.getConstructionorganization());
					ps.setString(7, buildlog.getConstructionorganizationcontacts());
					ps.setString(8, buildlog.getAccessory());
					ps.setString(9, buildlog.getReplenishaccessory());
					ps.setString(10, buildlog.getIsagree());
					ps.setString(11, buildlog.getReplenishreason());
					ps.setString(12, buildlog.getReplenishtime());
					ps.setString(13, buildlog.getAgreetime());
					ps.setString(14, buildlog.getChangemoney());
					ps.setString(15, buildlog.getLastendtime());
					ps.setString(16, buildlog.getAgreedoc());
					ps.setString(17, buildlog.getState());
					ps.setString(18, buildlog.getStatedetail());
					ps.setString(19, buildlog.getIschange());
					ps.setString(20, buildlog.getSafepollingdoc());
					ps.setString(21, buildlog.getChangereason());
					ps.setString(22, buildlog.getChangeendtime());
					ps.setString(23, buildlog.getChecktime());
					ps.setString(24, buildlog.getChecker());
					ps.setString(25, buildlog.getCheckmaterials());

					
					num=ps.executeUpdate();//执行sql语句
					//关闭执行对象
					ps.close();
					con.close();
					}catch(SQLException e){
						e.printStackTrace();
					}
					return num;
				
	}

	/**
	 * 根据主键更新信息
	 * @param buildlog
	 * @return
	 */
	
	public int update(Buildlog buildlog){
		System.out.println("进入执行更新sql语句方法");
		int isSuccess=0;//修改未成功
		/*String sql="UPDATE buildlog SET projectprincipal=? WHERE buildlogid=?";//修改语句*/
		String sql="UPDATE buildlog SET buildingsite=?,begintime=?,endtime=?,projectprincipal=?,price=?,constructionorganization=?, constructionorganizationcontacts=?,accessory=? WHERE buildlogid=?";
        /*isagree=?,replenishaccessory=?,replenishreason=?,replenishtime=?,agreetime=?,changemoney=?,lastendtime=?,agreedoc=?,state=?,statedetail=?,ischange=?,safepollingdoc=?,changereason=?,changeendtime=?,checktime=?,checker=?,checkmaterials=?*/
		System.out.println(sql);
		System.out.println(buildlog.getBuildingsite());
		PreparedStatement ps;
		try{
			//调用连接数据库的方法
			con=conn.getCon();
			//返回一个PrepareStatement实例
			ps=con.prepareStatement(sql);



			//设置修改的数据
			ps.setString(1, buildlog.getBuildingsite());
			ps.setString(2, buildlog.getBegintime());
			ps.setString(3, buildlog.getEndtime());
			ps.setString(4 , buildlog.getProjectprincipal());
			ps.setString(5, buildlog.getPrice());
			ps.setString(6, buildlog.getConstructionorganization());
			ps.setString(7, buildlog.getConstructionorganizationcontacts());
			ps.setString(8, buildlog.getAccessory());
			/*ps.setString(9, buildlog.getReplenishaccessory());
			ps.setString(10, buildlog.getIsagree());
			ps.setString(11, buildlog.getReplenishreason());
			ps.setString(12, buildlog.getReplenishtime());
			ps.setString(13, buildlog.getAgreetime());
			ps.setString(14, buildlog.getChangemoney());
			ps.setString(15, buildlog.getLastendtime());
			ps.setString(16, buildlog.getAgreedoc());
			ps.setString(17, buildlog.getState());
			ps.setString(18, buildlog.getStatedetail());
			ps.setString(19, buildlog.getIschange());
			ps.setString(20, buildlog.getSafepollingdoc());
			ps.setString(21, buildlog.getChangereason());
			ps.setString(22, buildlog.getChangeendtime());
			ps.setString(23, buildlog.getChecktime());
			ps.setString(24, buildlog.getChecker());
			ps.setString(25, buildlog.getCheckmaterials());*/
			ps.setInt(9, buildlog.getBuildlogid());


		    System.out.println(sql);
			/*System.out.println(Buildingsite);
			System.out.println(Begintime);
			System.out.println(Endtime);*//*
			System.out.println(buildlog.getProjectprincipal());
			System.out.println(buildlog.getPrice());
			System.out.println(buildlog.getConstructionorganization());
			System.out.println(buildlog.getConstructionorganizationcontacts());
			System.out.println(buildlog.getAccessory());
			System.out.println(buildlog.getReplenishaccessory());
			System.out.println(buildlog.getReplenishreason());
			System.out.println(buildlog.getReplenishtime());
			System.out.println(buildlog.getBuildlogid());*/
			//			//执行修改
			isSuccess=ps.executeUpdate();
			//关闭执行对象
			ps.close();
			con.close();
		}catch (SQLException e){
			e.printStackTrace();
		}
		return isSuccess;
	}
//
//	/*
//	 * 根据主键删除信息
//	 * */
//
	public int del(Buildlog buildlog){
		int isDel=0;//删除未成功
		//创建执行sql语句
		String sql="DELETE FROM buildlog WHERE buildlogid=?";
		//创建执行sql的对象
		PreparedStatement ps;
		try{
			con=conn.getCon();
			ps=con.prepareStatement(sql);
			//设置删除条件
			ps.setInt(1, buildlog.getBuildlogid());
			//删除
			isDel=ps.executeUpdate();
			//关闭执行对象
			ps.close();
			//关闭连接对象
			con.close();
		}catch (SQLException e){
			e.printStackTrace();
		}

		return isDel;
	}
//
//	/*
//	 * 根据查询条件返回查询结果
//	 *
//	 * */
	public List<Buildlog> getAll(Buildlog buildlog){
		System.out.println("进入查询方法");
		List<Buildlog> listBuildlog=new ArrayList<Buildlog>();
		//创建sql字符串
		String str="SELECT * FROM buildlog ";
//		buildlog.buildlogid,buildlog.buildingsite,buildlog.begintime,buildlog.endtime,buildlog.projectprincipal,buildlog.price,buildlog.constructionorganization,buildlog.constructionorganizationcontacts,buildlog.accessory,buildlog.replenishaccessory,buildlog.replenishreason,buildlog.isagree,buildlog.replenishtime
		int Buildlogid=0;
		/*if(buildlog.getBuildlogid()!=null){
			Buildlogid=buildlog.getBuildlogid();
		}*/
		con=conn.getCon();
		try{
			//创建执行sql的对象
			PreparedStatement ps=con.prepareStatement(str);
			System.out.println("查询语句:"+str);
			//设置查询条件
//			ps.setInt(1, Buildlogid);
			//返回查询记录结果的记录集
			ResultSet rs=ps.executeQuery();

			//将记录集存入集合当中
			while(rs.next()){
				//声明一个实体对象
				Buildlog b=new Buildlog();
				//给对象赋值
				b.setBuildlogid(rs.getInt(1));
				b.setBuildingsite(rs.getString(2));
				b.setBegintime(rs.getString(3));
				b.setEndtime(rs.getString(4));
				b.setProjectprincipal(rs.getString(5));
				b.setPrice(rs.getString(6));
				b.setConstructionorganization(rs.getString(7));
				b.setConstructionorganizationcontacts(rs.getString(8));
				b.setAccessory(rs.getString(9));
				b.setReplenishaccessory(rs.getString(10));
				b.setReplenishreason(rs.getString(11));
				b.setIsagree(rs.getString(12));
				b.setReplenishtime(rs.getString(13));
				b.setAgreetime(rs.getString(14));
				b.setChangemoney(rs.getString(15));
				b.setLastendtime(rs.getString(16));
				b.setAgreedoc(rs.getString(17));
				b.setState(rs.getString(18));
				b.setStatedetail(rs.getString(19));
				b.setIschange(rs.getString(20));
				b.setSafepollingdoc(rs.getString(21));
				b.setChangereason(rs.getString(22));
				b.setChangeendtime(rs.getString(23));
				b.setChecktime(rs.getString(24));
				b.setChecker(rs.getString(25));
				b.setCheckmaterials(rs.getString(26));
				//将对象实体添加到集合中
				listBuildlog.add(b);

			}
			//关闭记录集
			rs.close();
			//关闭执行的sql对象
			ps.close();
			//关闭连接
			con.close();

		}catch(SQLException e){
			e.printStackTrace();
		}
		System.out.println(listBuildlog);
		return listBuildlog;
	}




	public List<Buildlog> getDan(Buildlog buildlog){
		System.out.println("进入单项查询方法");
		List<Buildlog> listBuildlog=new ArrayList<Buildlog>();
		//创建sql字符串


		//创建sql字符串/
		String str="SELECT * from buildlog where projectprincipal=?";

		con=conn.getCon();
		try{
			//创建执行sql的对象
			PreparedStatement ps=con.prepareStatement(str);
			System.out.println("查询语句:"+str);
			//设置查询条件
			ps.setString(1,buildlog.getProjectprincipal());
			//返回查询记录结果的记录集
			ResultSet rs=ps.executeQuery();

			//将记录集存入集合当中
			while(rs.next()){
				//声明一个实体对象
				Buildlog b=new Buildlog();
				//给对象赋值
				b.setBuildlogid(rs.getInt(1));
				b.setBuildingsite(rs.getString(2));
				b.setBegintime(rs.getString(3));
				b.setEndtime(rs.getString(4));
				b.setProjectprincipal(rs.getString(5));
				b.setPrice(rs.getString(6));
				b.setConstructionorganization(rs.getString(7));
				b.setConstructionorganizationcontacts(rs.getString(8));
				b.setAccessory(rs.getString(9));
				b.setReplenishaccessory(rs.getString(10));
				b.setReplenishreason(rs.getString(11));
				b.setIsagree(rs.getString(12));
				b.setReplenishtime(rs.getString(13));
				b.setAgreetime(rs.getString(14));
				b.setChangemoney(rs.getString(15));
				b.setLastendtime(rs.getString(16));
				b.setAgreedoc(rs.getString(17));
				b.setState(rs.getString(18));
				b.setStatedetail(rs.getString(19));
				b.setIschange(rs.getString(20));
				b.setSafepollingdoc(rs.getString(21));
				b.setChangereason(rs.getString(22));
				b.setChangeendtime(rs.getString(23));
				b.setChecktime(rs.getString(24));
				b.setChecker(rs.getString(25));
				b.setCheckmaterials(rs.getString(26));
				//将对象实体添加到集合中
				listBuildlog.add(b);

			}
			//关闭记录集
			rs.close();
			//关闭执行的sql对象
			ps.close();
			//关闭连接
			con.close();

		}catch(SQLException e){
			e.printStackTrace();
		}
		System.out.println(listBuildlog);
		return listBuildlog;
	}


    public List<Buildlog> getAddc(Buildlog buildlog){
        System.out.println("根据ID进行查询");
        List<Buildlog> listBuildlog=new ArrayList<Buildlog>();
        //创建sql字符串
        /*String str="SELECT * FROM buildlog WHERE ";*/
//		buildlog.buildlogid,buildlog.buildingsite,buildlog.begintime,buildlog.endtime,buildlog.projectprincipal,buildlog.price,buildlog.constructionorganization,buildlog.constructionorganizationcontacts,buildlog.accessory,buildlog.replenishaccessory,buildlog.replenishreason,buildlog.isagree,buildlog.replenishtime
        /*int Buildlogid=0;*/
		/*if(buildlog.getBuildlogid()!=null){
			Buildlogid=buildlog.getBuildlogid();
		}*/
        //创建sql字符串/
        System.out.println(buildlog.getBuildlogid());
        String str="SELECT * from buildlog where buildlogid=?";

        con=conn.getCon();
        try{
            //创建执行sql的对象
            PreparedStatement ps=con.prepareStatement(str);
            System.out.println("查询语句:"+str);
            //设置查询条件
            ps.setInt(1,buildlog.getBuildlogid());
            //返回查询记录结果的记录集
            ResultSet rs=ps.executeQuery();
            System.out.println(buildlog.getBuildlogid());
            //将记录集存入集合当中
            while(rs.next()){
                //声明一个实体对象
                Buildlog b=new Buildlog();
                //给对象赋值
                b.setBuildlogid(rs.getInt(1));
                b.setBuildingsite(rs.getString(2));
                b.setBegintime(rs.getString(3));
                b.setEndtime(rs.getString(4));
                b.setProjectprincipal(rs.getString(5));
                b.setPrice(rs.getString(6));
                b.setConstructionorganization(rs.getString(7));
                b.setConstructionorganizationcontacts(rs.getString(8));
                b.setAccessory(rs.getString(9));
                b.setReplenishaccessory(rs.getString(10));
                b.setReplenishreason(rs.getString(11));
                b.setIsagree(rs.getString(12));
                b.setReplenishtime(rs.getString(13));
                b.setAgreetime(rs.getString(14));
                b.setChangemoney(rs.getString(15));
                b.setLastendtime(rs.getString(16));
                b.setAgreedoc(rs.getString(17));
                b.setState(rs.getString(18));
                b.setStatedetail(rs.getString(19));
                b.setIschange(rs.getString(20));
                b.setSafepollingdoc(rs.getString(21));
                b.setChangereason(rs.getString(22));
                b.setChangeendtime(rs.getString(23));
                b.setChecktime(rs.getString(24));
                b.setChecker(rs.getString(25));
                b.setCheckmaterials(rs.getString(26));
                //将对象实体添加到集合中
                listBuildlog.add(b);

            }
            //关闭记录集
            rs.close();
            //关闭执行的sql对象
            ps.close();
            //关闭连接
            con.close();

        }catch(SQLException e){
            e.printStackTrace();
        }
        System.out.println(listBuildlog);
        return listBuildlog;
    }
}
